package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductCategoryQualification;
import com.erp.gather.domain.bo.ErpCollectProductCategoryQualificationBo;
import com.erp.gather.domain.vo.ErpCollectProductCategoryQualificationVo;
import com.erp.gather.mapper.ErpCollectProductCategoryQualificationMapper;
import com.erp.gather.service.IErpCollectProductCategoryQualificationService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 类目资质信息Service业务层处理
 *
 * @author erp
 * @date 2023-10-31
 */
@RequiredArgsConstructor
@Service
public class ErpCollectProductCategoryQualificationServiceImpl implements IErpCollectProductCategoryQualificationService {

    private final ErpCollectProductCategoryQualificationMapper baseMapper;

    /**
     * 查询类目资质信息
     */
    @Override
    public ErpCollectProductCategoryQualificationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询类目资质信息列表
     */
    @Override
    public TableDataInfo<ErpCollectProductCategoryQualificationVo> queryPageList(ErpCollectProductCategoryQualificationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpCollectProductCategoryQualification> lqw = buildQueryWrapper(bo);
        Page<ErpCollectProductCategoryQualificationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询类目资质信息列表
     */
    @Override
    public List<ErpCollectProductCategoryQualificationVo> queryList(ErpCollectProductCategoryQualificationBo bo) {
        LambdaQueryWrapper<ErpCollectProductCategoryQualification> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpCollectProductCategoryQualification> buildQueryWrapper(ErpCollectProductCategoryQualificationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpCollectProductCategoryQualification> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getCategoryId()), ErpCollectProductCategoryQualification::getCategoryId, bo.getCategoryId());
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), ErpCollectProductCategoryQualification::getCategoryName, bo.getCategoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getLabel()), ErpCollectProductCategoryQualification::getLabel, bo.getLabel());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ErpCollectProductCategoryQualification::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getQualificationKey()), ErpCollectProductCategoryQualification::getQualificationKey, bo.getQualificationKey());
        lqw.eq(StringUtils.isNotBlank(bo.getTips()), ErpCollectProductCategoryQualification::getTips, bo.getTips());
        lqw.eq(StringUtils.isNotBlank(bo.getRequired()), ErpCollectProductCategoryQualification::getRequired, bo.getRequired());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpCollectProductCategoryQualification::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增类目资质信息
     */
    @Override
    public Boolean insertByBo(ErpCollectProductCategoryQualificationBo bo) {
        ErpCollectProductCategoryQualification add = BeanUtil.toBean(bo, ErpCollectProductCategoryQualification.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改类目资质信息
     */
    @Override
    public Boolean updateByBo(ErpCollectProductCategoryQualificationBo bo) {
        ErpCollectProductCategoryQualification update = BeanUtil.toBean(bo, ErpCollectProductCategoryQualification.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpCollectProductCategoryQualification entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除类目资质信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 批量删除类目资质信息
     */
    @Override
    public R<Object> selectQualificationByCategoryId(Long categoryId) {
        return R.ok(baseMapper.selectList(
                new LambdaQueryWrapper<ErpCollectProductCategoryQualification>()
                        .eq(ErpCollectProductCategoryQualification::getCategoryId, categoryId)
        ));
    }
}
